package com.ruoyi.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.course.domain.SHomework;
import com.ruoyi.course.vo.SHomeworkVo;
import com.ruoyi.course.mapper.SHomeworkMapper;
import com.ruoyi.course.service.ISHomeworkService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.teacher.domain.THomework;
import com.ruoyi.teacher.mapper.THomeworkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生作业Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class SHomeworkServiceImpl extends ServiceImpl<SHomeworkMapper, SHomework> implements ISHomeworkService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    THomeworkMapper homeworkMapper;
    @Override
    public TableDataInfo<SHomeworkVo> queryList(SHomeworkVo entity) {
        Page<SHomeworkVo> sHomeworkVoPage = this.baseMapper.queryList(PageUtils.buildPage(), entity);
        List<SHomeworkVo> records = sHomeworkVoPage.getRecords();
        for (SHomeworkVo record : records) {
//            查询用户名
            String username = null;
            LambdaQueryWrapper<SysUser> userqw = new LambdaQueryWrapper<>();
            userqw.eq(SysUser::getUserId,record.getsId());
            SysUser sysUser = sysUserMapper.selectOne(userqw);
            if (sysUser!=null){
                username = sysUser.getNickName();
            }
//            查询作业名
            String homeworkName = null;
            LambdaQueryWrapper<THomework> hqw = new LambdaQueryWrapper<>();
            hqw.eq(THomework::getId,record.gethId());
            THomework tHomework = homeworkMapper.selectOne(hqw);
            if (tHomework!=null){
                homeworkName = tHomework.getName();
            }
            record.setStudentName(username);
            record.setHomeworkName(homeworkName);
        }
        return PageUtils.buildDataInfo(sHomeworkVoPage);
    }

    @Override
    public List<SHomeworkVo> queryAll(SHomeworkVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public SHomeworkVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }
}
